package com.yao.code;

/**
 * @author yaoquanwei
 * @date 2021/8/30 22:15
 */
public class Code210830 {
    /**
     * 剑指offer 39.数组中出现次数超过一半的数字
     *
     * @param nums
     * @return
     */
    public int majorityElement(int[] nums) {
        int count = 0;
        Integer card = null;
        for (int num : nums) {
            if (count == 0) {
                card = num;
            }
            count += (card == num) ? 1 : -1;
        }
        return card;
    }

    /**
     * 剑指offer 66.构建乘积数组
     *
     * @param a
     * @return
     */
    public int[] constructArr(int[] a) {
        int n = a.length;
        int[] res = new int[n];
        for (int i = 0, j = 1; i < n; i++) {
            res[i] = j;
            j *= a[i];
        }
        for (int i = n - 1, j = 1; i >= 0; i--) {
            res[i] *= j;
            j *= a[i];
        }
        return res;
    }
}
